# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=xapian-core
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
epoch=1
pkgver=1.4.23
pkgrel=1
pkgdesc='Open source search engine library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.xapian.org/'
msys2_references=(
  "cpe: cpe:/a:xapian:xapian-core"
)
license=('spdx:GPL-2.0-or-later')
# xapian config requires libxapian.la
options=('libtool')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://oligarchy.co.uk/xapian/${pkgver}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "dont-set-msvcrt-version.patch")
sha256sums=('30d3518172084f310dab86d262b512718a7f9a13635aaa1a188e61dc26b2288c'
            'SKIP'
            '8d145b42f623db0db97c335627cfbc43abbd5bcae8ea6e45e5e9f89000361b71')
validpgpkeys=('08E2400FF7FE8FEDE3ACB52818147B073BAD2B07') # Olly Betts <olly@debian.org>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/dont-set-msvcrt-version.patch
  autoreconf -fiv
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  CXXFLAGS+=" -std=gnu++14"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

package_xapian-core() {
  cd "${srcdir}"/build-${MSYSTEM}
  make install DESTDIR="${pkgdir}"

  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc

  install -vDm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}

package_xapian-core-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  cd "${srcdir}"/build-${MSYSTEM}
  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
